/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 *
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl-3.0.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.model.entity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import xperiment.metaphor.model.AbstractBean;
import xperiment.metaphor.model.XML;

/**
 * Logon history
 *
 * @author Mark Ashworth
 * @version 1.0.0
 * @since 1.0.0
 */
//@Entity
//@Table(name = "login_history")
public class LoginHistory extends AbstractBean implements Serializable, XML, Comparable {
    private static final long serialVersionUID = 1L;

     /* The date of the logon */
    //@Column(name = "when", nullable = false)
    private Date date;

    /* The location of the access (like IP Address or computer host name) */
    //@Column(name = "location", nullable = true, length = 255)
    private String location;

    /**
     * Default constructor
     */
    public LoginHistory() {
        super();
    }

    /**
     * Constructs the login information
     * @param date The date of the login
     * @param location The location of the login
     */
    public LoginHistory(Date date, String location) {
        this.date = date;
        this.location = location;
    }

    /**
     * The date of the logon
     * @return Date
     */
    public Date getDate() {
        return date;
    }

    /**
     * The location of the access (like IP Address or computer host name)
     * @return String
     */
    public String getLocation() {
        return location;
    }

    public int compareTo(Object o) {

        LoginHistory other = (LoginHistory) o;

        return new CompareToBuilder()
                .append(this.date, other.getDate())
                .append(this.location, other.getLocation())
                .toComparison();
    }

    @Override
    public boolean equals(Object obj) {

        if (obj == null) { return false; }
        if (obj == this) { return true; }
        if (obj.getClass() != getClass()) {
            return false;
        }
        LoginHistory rhs = (LoginHistory) obj;
        return new EqualsBuilder()
            .append(this.date, rhs.getDate())
            .append(this.location, rhs.getLocation())
            .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 43)
                .append(this.date)
                .append(this.location)
                .toHashCode();
    }

    /**
     * Returns a String representation of the object
     * @return String
     */
    @Override
    public String toString() {

        return "login-history: "
                + "' date='" + (date != null ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getDate()) : "")
                + "' location='" + getLocation()
                ;
    }

    /**
     * Returns a XML representation of the object
     * @return String
     */
    public String toXML() {

        return "<login-history class='" + getClass().getName()
                + "' date='" + (date != null ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getDate()) : "")
                + "' location='" + getLocation()
                + "' />";
    }
}
